﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapInfo.MiPro.Interop;
using System.Windows.Forms;

namespace CustomMapinfoWindows.ViewModels
{
    public class TabFileViewModel : MapinfoFileViewModel
    {
        public TabFileViewModel(string filePath, TreeViewItemViewModel parent, bool pathChecking)
            : base(filePath, parent, false, pathChecking)
        { }

        public override void OpenFile()
        {
            InteropServices.MapInfoApplication.Do("Open Table \"" + this.FullPath + "\"");
        }

        private string namestore;

        public void ResolvePath()
        { 
            
        }

        public List<MenuItem> MenuOptions
        {
            get
            {
                var menu = new List<MenuItem>();
                menu.Add(new MenuItem("Open") { Command = new DelegateCommand(OpenFile) });
                menu.Add(new MenuItem("Edit Name") { Command = new DelegateCommand(() => this.IsEditing = true) });
                menu.Add(new MenuItem("Resolve Path") { Command = new DelegateCommand(ResolvePath) });
                return menu;
            }
        }

        public override bool IsSelected
        {
            get
            {
                return base.IsSelected;
            }
            set
            {
                if (!value)
                    this.IsEditing = false;

                base.IsSelected = value;
            }
        }

        private bool isediting;
        public bool IsEditing
        {
            get
            {
                return this.isediting;
            }
            set
            {
                // Store the old names because we are in edit mode.
                if (!this.IsEditing && value)
                    this.namestore = this.Name;

                //Compare the old name to see if it has changed.
                if (this.IsEditing && !value)
                { 
                    if (String.Compare(this.Name,this.namestore) != 0)
                        this.RenameFile();
                }

                this.isediting = value;
                base.OnPropertyChanged("IsEditing");
            }
        }

        private void RenameFile()
        {
            MessageBox.Show(this.namestore + " will become " + this.Name);
        }
    }
}
